package com.simpleactors;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Dmitry
 */
public class ActorSystem {
    NullActor nullActor;
    private final Map<String, Actor> actors = Collections.synchronizedMap(new HashMap<String, Actor>());

    public ActorSystem() {
        nullActor = new NullActor();
    }

    public ActorSystem addActor(Actor actor) {
        if (actors.containsKey(actor.getName())) {
            throw new RuntimeException("Actor with name " + actor.getName() + " already exists");
        }
        actor.setActorSystem(this);
        actors.put(actor.getName(), actor);
        actor.start();
        return this;
    }

    public void removeActor(String name) {
        Actor actor = actors.get(name);
        if (actor != null) {
            actor.stop();
            actors.remove(name);
        }
    }

    public Actor getActor(String actorName) {
        return actors.get(actorName);
    }
}
